#include"Queue.h"
void QueueInit(Queue* qt)//初始化
{
	assert(qt);
	qt->head = qt->tail = NULL;
}
void QueueDestroy(Queue* qt)//销毁队列
{
	assert(qt);
	QueueNode* cur = qt->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	qt->head == qt->tail==NULL;
}
void QueuePush(Queue* qt, QDataType x)//队尾插入
{
	assert(qt);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		printf("内存不足\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;

	if (qt->head == NULL)
	{
		qt->head=qt->tail= newnode;
	}
	else
	{
		qt->tail->next = newnode;
		qt->tail = newnode;
	}
}
void QueuePop(Queue* qt)//队尾删除
{
	assert(qt);
	QueueNode* next = qt->head->next;
	free(qt->head);
	qt->head = next;
	if (qt->head == NULL)//防止队尾变成野指针
	{
		qt->tail = NULL;
	}
}
QDataType QueueFront(Queue* qt)//获取队头元素
{
	assert(qt);
	assert(qt->head);
	return qt->head->data;

}
QDataType QueueTail(Queue* qt)//获取队尾元素
{
	assert(qt);
	assert(qt->tail);
	return qt->tail->data;
}
int QueueSize(Queue* qt)//队列长度
{
	assert(qt);
	int size = 0;
	QueueNode* cru = qt->head;
	while (cru)
	{
		size++;
		cru = cru->next;
	}
	return size;
}
int QueueEmpty(Queue* qt)//判断队列是否为空
{
	assert(qt);
	return qt->head == NULL ? 1 : 0;
}